<?php
/**
*
* @version $Id: update_module_databasepro.php 2009-10-25 17:11:26Z IOsetting $
* @copyright (c) 2005 phpbbchina.com
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/*
* Usage: 
*
* Install: http://Your forum root url/update_module_databasepro.php?m=install 
* Unistall: http://Your forum root url/update_module_databasepro.php?m=uninstall
*
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

$scriptname = 'update_module_xspace.php';

$mode   = request_var('m', 'install');

echo dheader();

switch ($mode) {
	case 'uninstall':
		uninstall_xspace();
	break;

	case 'install':
	default:
		uninstall_xspace();
		
		$sql = 'SELECT max(right_id) AS max_right_id 
			FROM ' . MODULES_TABLE . '
			WHERE module_class = "ucp"';
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result); 
		$left_base_id = $row['max_right_id']; 
		$db->sql_freeresult($result);
		
		$sql = 'INSERT INTO `phpbb_modules` 
			(`module_enabled`, 
			`module_display`, 
			`module_basename`, 
			`module_class`, 
			`parent_id`, 
			`left_id`, 
			`right_id`, 
			`module_langname`, 
			`module_mode`, 
			`module_auth`) 
			VALUES 
			(1, 
			1, 
			\'\', 
			\'ucp\', 
			0, 
			' . ($left_base_id + 1) . ', 
			' . ($left_base_id + 6) . ', 
			\'UCP_XSPACE\', 
			\'\', 
			\'\')';
		$db->sql_query($sql);
		$parent_id = $db->sql_nextid();
		
		$sql = 'INSERT INTO `phpbb_modules` 
			(`module_enabled`, 
			`module_display`, 
			`module_basename`, 
			`module_class`, 
			`parent_id`, 
			`left_id`, 
			`right_id`, 
			`module_langname`, 
			`module_mode`, 
			`module_auth`) 
			VALUES 
			(1, 
			1, 
			\'xspace\', 
			\'ucp\', 
			' . $parent_id . ', 
			' . ($left_base_id + 2) . ', 
			' . ($left_base_id + 3) . ', 
			\'UCP_XSPACE_PROFILE_INFO\', 
			\'profile_info\', 
			\'\')';
		$db->sql_query($sql);
		
		$sql = 'INSERT INTO `phpbb_modules` 
			(`module_enabled`, 
			`module_display`, 
			`module_basename`, 
			`module_class`, 
			`parent_id`, 
			`left_id`, 
			`right_id`, 
			`module_langname`, 
			`module_mode`, 
			`module_auth`) 
			VALUES 
			(1, 
			1, 
			\'xspace\', 
			\'ucp\', 
			' . $parent_id . ', 
			' . ($left_base_id + 4) . ', 
			' . ($left_base_id + 5) . ', 
			\'UCP_XSPACE_CATEGORIES\', 
			\'categories\', 
			\'\')';
		$db->sql_query($sql);
		echo "UCP / Xspace module installed<br />";

	break;
}

function uninstall_xspace() {
	global $db;

	$sql = 'SELECT COUNT(module_id) AS num_xspace
	   FROM ' . MODULES_TABLE . '
	   WHERE module_langname like "UCP_XSPACE%" AND module_class = "ucp"';
	$result = $db->sql_query($sql);
	$xspace_installed = (int) $db->sql_fetchfield('num_xspace');
	$db->sql_freeresult($result);

	if ($xspace_installed) {
		$sql = 'SELECT left_id, right_id 
			FROM ' . MODULES_TABLE . '
			WHERE module_langname = "UCP_XSPACE" AND parent_id = 0 AND module_class = "ucp"';
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$sql = 'UPDATE ' . MODULES_TABLE . ' SET 
				left_id = left_id - ' . $xspace_installed * 2 . ', 
				right_id = right_id - ' . $xspace_installed * 2 . ' 
			WHERE left_id > ' . $row['right_id'] . ' AND module_class = "ucp"';
		$db->sql_query($sql);

		$sql = 'DELETE FROM ' . MODULES_TABLE . ' 
			WHERE module_langname like "UCP_XSPACE%" AND module_class = "ucp"';
		$db->sql_query($sql);
		echo "UCP / Xspace exists, uninstalled<br />";
	} else {
		echo "UCP / Xspace doesn't exist, skipped<br />";
	}
}

echo dfooter();

function dheader() {
     return "
  <html>
  <head>
  <title>Database Pro Installer/Uninstaller</title>
  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">
  <style>
  A:visited   {COLOR: #3A4273; TEXT-DECORATION: none}
  A:link      {COLOR: #3A4273; TEXT-DECORATION: none}
  A:hover      {COLOR: #3A4273; TEXT-DECORATION: underline}
  body,table,td   {COLOR: #3A4273; FONT-FAMILY: Tahoma, Verdana, Arial; FONT-SIZE: 12px; LINE-HEIGHT: 20px; scrollbar-base-color: #E3E3EA; scrollbar-arrow-color: #5C5C8D}
  input      {COLOR: #085878; FONT-FAMILY: Tahoma, Verdana, Arial; FONT-SIZE: 12px; background-color: #3A4273; color: #FFFFFF; scrollbar-base-color: #E3E3EA; scrollbar-arrow-color: #5C5C8D}
  .install   {FONT-FAMILY: Arial, Verdana; FONT-SIZE: 20px; FONT-WEIGHT: bold; COLOR: #000000}
  </style>
  </head>
  <body bgcolor=\"#3A4273\" text=\"#000000\">
  <table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#FFFFFF\" align=\"center\">
    <tr>
      <td>
        
     ";
}

function dfooter() {
     return "
      </td>
    </tr>
  </table>
  <br>
  </body>
  </html>
     ";
}
?>
